import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity()
export class Log {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false })
  uid: number; // 用户ID，0表示系统操作

  @Column({ nullable: false })
  username: string; // 用户名

  @Column({ 
    type: 'int', 
    default: 1,
    comment: '日志类型：1=管理员操作, 2=登录日志, 3=安全事件, 4=系统事件'
  })
  type: number;

  @Column({ 
    type: 'json', 
    nullable: true,
    comment: '日志内容JSON，存储灵活的数据结构'
  })
  logContent: any;

  @Column({ 
    nullable: false,
    comment: '操作类型，如：create, update, delete, login, logout等'
  })
  operationType: string;

  @Column({ 
    nullable: true,
    comment: '目标类型，如：文章、用户、评论等'
  })
  targetType: string;

  @Column({ 
    type: 'int', 
    nullable: true,
    comment: '目标对象ID'
  })
  targetId: number;

  @Column({ 
    nullable: true,
    comment: '目标对象名称'
  })
  targetName: string;

  @Column({ 
    nullable: true,
    comment: 'IP地址'
  })
  ipAddress: string;

  @Column({ 
    nullable: true,
    comment: '用户代理'
  })
  userAgent: string;

  @Column({ 
    nullable: true,
    comment: '设备信息'
  })
  deviceInfo: string;

  @Column({ 
    nullable: true,
    comment: '地理位置信息'
  })
  location: string;

  @Column({ 
    type: 'int',
    default: 1,
    comment: '操作状态：1=成功, 2=失败, 3=警告'
  })
  status: number;

  @Column({ 
    nullable: true,
    comment: '失败原因或其他详细信息'
  })
  failureReason: string;

  @Column({ 
    type: 'int',
    default: 0,
    comment: '安全级别：0=普通, 1=警告, 2=严重, 3=紧急'
  })
  securityLevel: number;

  @Column({ 
    type: 'int',
    default: 0,
    comment: '是否已读：0=未读, 1=已读'
  })
  isRead: number;

  @Column({ 
    type: 'int',
    default: 0,
    comment: '是否已处理：0=未处理, 1=已处理'
  })
  isResolved: number;

  @Column({ 
    nullable: true,
    comment: '处理说明'
  })
  resolution: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  // 用于查询的索引字段
  @Column({ 
    type: 'timestamp',
    nullable: true,
    comment: '事件发生时间，用于查询和排序'
  })
  eventTime: Date;
}